//
//  LLStatusCell.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/2.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLStatusCell.h"
#import "LLStatusLayout.h"
#import "LLStatusModel.h"

@interface LLStatusCell ()
@property (strong, nonatomic) UILabel *contentTextLbl;
@property (strong, nonatomic) NSMutableArray *picViews;
@property (strong, nonatomic) UILabel *bottomInfoLbl;
@property (strong, nonatomic) LLButton *editBtn;
@end

@implementation LLStatusCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    _contentTextLbl = [LLInitBaseViewTool labelWithText:nil textColor:UIColor.blackColor font:19 textAlignment:NSTextAlignmentLeft];
    _contentTextLbl.numberOfLines = 3;
    [self.contentView addSubview:_contentTextLbl];
    
    self.picViews = NSMutableArray.array;
    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imgView = UIImageView.new;
        imgView.layer.cornerRadius = 2;
        imgView.clipsToBounds = YES;
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:imgView];
        [self.picViews addObject:imgView];
    }
    
    _bottomInfoLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(131) font:11 textAlignment:NSTextAlignmentLeft];
    [self.contentView addSubview:_bottomInfoLbl];
    
    _editBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"close_edit" titleColor:nil font:0];
    [_editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_editBtn];
}

- (void)setLayout:(LLStatusLayout *)layout {
    _layout = layout;
    LLStatusModel *model = layout.model;
    _contentTextLbl.frame = layout.contentTextFrame;

    for (NSInteger i = 0; i < self.picViews.count; i++) {
        UIImageView *imgView = self.picViews[i];

        NSInteger imgcount = 0;
        if ([model.img_url isKindOfClass:[NSArray class]]) {
            imgcount = model.img_url.count;
        }

        if ( (imgcount == 0) || (i > imgcount-1)) {
            imgView.hidden = YES;
        }
        else {
            imgView.hidden = NO;

            if (model.img_url.count == 1) {

                imgView.frame = CGRectFromString(layout.imgFrames[0]);
                [imgView sd_setImageWithURL:LLImageWithUrl(model.img_url[0]) placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageRefreshCached];
            }
            else if (model.img_url.count > 1) {

                if (i <= model.img_url.count) {

                    imgView.frame = CGRectFromString(layout.imgFrames[0]);
                    [imgView sd_setImageWithURL:LLImageWithUrl(model.img_url[i]) placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageRefreshCached];
                }
            }
            else{

                imgView.frame = CGRectMake(0, 0, 0, 0);
            }

//            if (i == layout.imgFrames.count -1) {
//                if (model.img_url.count != 0) {
//                    imgView.frame = CGRectFromString(layout.imgFrames[i]);
//                    [imgView sd_setImageWithURL:LLImageWithUrl(model.img_url[i]) placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageRefreshCached];
//                }else{
//
//                    imgView.frame = CGRectMake(0, 0, 0, 0);
//                }
//            }
        }
    }
    _bottomInfoLbl.frame = layout.bottomInfoFrame;
    _editBtn.frame = layout.editBtnFrame;
    
    _contentTextLbl.text = [NSString stringWithFormat:@"%@",model.newsTitle];
    _bottomInfoLbl.text = [NSString stringWithFormat:@"%@",model.look_num];
}

- (void)editBtnClick {
    
}

@end
